var PeopleMedia = PeopleMedia || {};
PeopleMedia.Search = new function () {
    this.init = function () {
        var clickCount = 0;
        $jq('div#menu > a[href^="/v3/Search"]').click(function (e) {
            e.preventDefault();
        });
        $jq('.searchbuttonrepeat').click(PeopleMedia.Search.CriteriaMoreClicked);
        PeopleMedia.Search.WireUpCriteria($jq('.searchparameteroption, .searchparameteroptionlast'));
        $jq('.searchparameteroption2, .searchparameteroptionlast2')
               .mouseenter(function () { $jq(this).addClass("searchparameteroptionhover"); })
               .mouseleave(function () { $jq(this).removeClass("searchparameteroptionhover"); });
        $jq('.searchmoreexpanded > div[criteria-type]').click(PeopleMedia.Search.AddCriteria);
        $jq('body').click(function () { $jq('.searchbuttonrepeat > div[class$=expandedplace] > div').hide(); });
        $jq('#HideSearchParametersLink, #ShowSearchParametersLink').click(PeopleMedia.Search.ToggleSearchParametersVisible);
        $jq('#MinAgeTextBox, #MaxAgeTextBox').keydown(PeopleMedia.Search.NumbersOnly).blur(PeopleMedia.Search.ValidateAge);
        $jq('#ZipTextBox').blur(PeopleMedia.Search.ValidateZip);

        $jq('.showhidearrowdown, .showhidearrowup').click(PeopleMedia.Search.ToggleSearchParametersVisible);
        $jq('input[type=text]').focus(function () { $jq(this).val(''); });
        $jq('#UsernameSearchTextBox').change(PeopleMedia.Search.NumbersAndLettersOnly).keypress(PeopleMedia.Search.NumbersAndLettersOnly).blur(function () {
            PeopleMedia.Search.NumbersAndLettersOnly();
            if ($jq('#UsernameSearchTextBox').val() == '') {
                $jq('#UsernameSearchTextBox').val($jq.bbq.getState('Username'));
            }
        });

        PeopleMedia.Search.defaultZip = $jq('#ZipTextBox').val();
        $jq('input[name=hasphotos]').change(function () {
            if ($jq('#HasPhotosAny').is(':checked')) {
                $jq('[data-property=HasPhotos]').html('have either');
            } else {
                $jq('[data-property=HasPhotos]').html('have photos');
            }
        });
        $jq('[data-input]').change(PeopleMedia.Search.CriteriaSelectionChanged);
        $jq('[hide-criteria]').click(PeopleMedia.Search.HideCriteria);
        $jq('#MinHeight, #MaxHeight').change(PeopleMedia.Search.UpdateHeight);
        PeopleMedia.Search.UpdateHeight();
        $jq('#Location').change(function () {
            $jq('[data-property=countryCode]').html('in ' + $jq('#Location option:selected').text());
        });

        $jq(window).on('hashchange', PeopleMedia.Search.hashchange);
        var state = $jq.bbq.getState();

        if (state != undefined && state.Username != undefined) {
            PeopleMedia.Search.SetUsernameData(state);
            PeopleMedia.Search.ShowUsernameSearch(true);
            if (state.Username != '' || state.ShowResult == 'true') {
                PeopleMedia.Search.doUsernameSearch(true);
            } else {
                $jq('#LoadingResultsDiv').hide();
            }
        } else {
            PeopleMedia.Search.ExecuteDefaultSearch();
        }
        $jq('#UsernameSearchTab a').click(function (e) {
            e.preventDefault();
            if (PeopleMedia.Search.isSearching == true) {
                return;
            }
            if (!PeopleMedia.Search.isFirstResult) {
                PeopleMedia.Search.ShowUsernameSearch();
            }
        });
        $jq('#OnlineNowSearchTab').click(function () {
            if (PeopleMedia.Search.isSearching == true) {
                return;
            }
            if (!PeopleMedia.Search.isFirstResult) {
                if (PeopleMedia.Search.isUsernameSearch == true) {
                    PeopleMedia.Search.HideUsernameSearch();
                }
                $jq.bbq.pushState({ Sortby: 'MembersOnline', Start: 1, IsDefaultSearch: false });
            }
        });
        PeopleMedia.Search.SetOnlineSearch();
        $jq('#ExecuteSearchButton').click(PeopleMedia.Search.SearchButtonClicked);

    };
    this.isSearching = false;
    this.isFirstResult = true;
    this.FeatureName = 'search';
    this.UsernameFeatureName = 'usernamesearch';
    this.sortCol = 'MyDefault';
    this.defaultMinAge = 18;
    this.defaultMaxAge = 120;
    this.currentPage = 1;
    this.viewGallery = false;
    this.slideToggleSpeed = 300;
    this.isChanging = false;
    this.defaultZip = '';
    this.InvalidZip = false;
    this.CriteriaSelectionChanged = function () {
        if (PeopleMedia.Search.isChanging == true) { return; }
        PeopleMedia.Search.isChanging = true;
        var criteriaName = $jq(this).attr('data-input');
        var value = $jq(this).attr('criteria-text');
        var $parent = $jq('[criteria-for=' + criteriaName + ']');
        //var display = $jq('[criteria-display=' + criteriaName + ']').html(value);
        $parent.find('input[type=checkbox]').unbind();
        var allcheckboxes = $parent.find('[data-input]').not('[criteria-text=any]');
        var anycheckbox = $parent.find('[data-input]').filter('[criteria-text=any]');
        if (value == 'any' && $jq(this).is(":checked")) {
            anycheckbox.attr('checked', true);
            allcheckboxes.removeAttr('checked');
        } else {
            var otherChecked = allcheckboxes.filter(":checked").length;
            if (otherChecked <= 0) {
                anycheckbox.attr('checked', true);
            } else if (otherChecked == allcheckboxes.length) {
                anycheckbox.attr('checked', true);
                allcheckboxes.removeAttr('checked');
            } else {
                anycheckbox.removeAttr('checked');
            }

        }
        $parent.find('input[type=checkbox]').change(PeopleMedia.Search.CriteriaSelectionChanged);
        PeopleMedia.Search.isChanging = false;

    };
    this.UpdateHeight = function () {
        var minVal = $jq('#MinHeight').val();
        var maxVal = $jq('#MaxHeight').val();

        var min = $jq('#MinHeight option:selected').text();
        var max = $jq('#MaxHeight option:selected').text();
        $jq('[criteria-display=height]').html('height: ' + min + '&quot; - ' + max + '&quot;');
    };
    this.ShowState = function () {
        $jq('#SearchCountry').show();
        $jq('#SearchZip').hide();
    };
    this.ShowZip = function () {
        $jq('#SearchCountry').hide();
        $jq('#SearchZip').show();
    };
    this.NumbersOnly = function (event) {
        if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
        // Allow: Ctrl+A
               (event.keyCode == 65 && event.ctrlKey === true) ||
        // Allow: home, end, left, right
                   (event.keyCode >= 35 && event.keyCode <= 39)) {
            // let it happen, don't do anything
            return;
        } else {
            // Ensure that it is a number and stop the keypress
            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
                event.preventDefault();
            }
        }
    };
    this.NumbersAndLettersOnly = function (event) {
        var text = $jq('#UsernameSearchTextBox').val();
        $jq('#UsernameSearchTextBox').val(text.replace(/[^a-z0-9]/gi, ''));
    };
    this.ValidateZip = function () {
        PeopleMedia.Search.InvalidZip = true;
        var zip = $jq('#ZipTextBox').val();
        if (zip == '') {
            $jq('#ZipTextBox').val(PeopleMedia.Search.defaultZip);
            zip = PeopleMedia.Search.defaultZip;
        }
        $jq('#ExecuteSearchButton').addClass('buttonunavailable').unbind();
        if (zip.length < 5) {

            $jq('.ziperrorplace').fadeIn(PeopleMedia.Search.slideToggleSpeed);
            return;
        }
        $jq('[data-property=ZipCode]').html('near ' + zip);
        
        var startTime = new Date().getTime();
        var url = '/v3/ajax/validatezip';
        var timeoutValue = 60000;
        $jq.ajax({
            type: "POST",
            url: url,
            data: { ZipCode: zip },
            dataType: "json",
            success: function (result) {
                if (result.redirect) {
                    location.href = result.redirect;
                } else if (result.status == "Success") {
                    $jq('.ziperrorplace').fadeOut(PeopleMedia.Search.slideToggleSpeed);
                    $jq('#ExecuteSearchButton').removeClass('buttonunavailable').click(PeopleMedia.Search.SearchButtonClicked);
                    PeopleMedia.Search.defaultZip = zip;
                    PeopleMedia.Search.InvalidZip = false;
                } else if (result.status != "Success") {
                    $jq('.ziperrorplace').fadeIn(PeopleMedia.Search.slideToggleSpeed);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                if (errorThrown === 'timeout' || (jqXHR.readyState !== 0 && jqXHR.status !== 0)) {
                    var endTime = new Date().getTime();
                    var duration = (endTime - startTime);
                    // PeopleMedia.Popups.ShowGenericMessage(PeopleMedia.Popups.AjaxErrorMessage());
                    PeopleMedia.Logger.LogAjaxErrorXHR("Ajax request failure", url, { method: "PeopleMedia.Search.ValidateZip", duration: duration }, jqXHR, textStatus, errorThrown, function () {
                    });
                }
            },
            async: true,
            cache: false,
            timeout: timeoutValue,
            suppressErrors: true
        });
    };
    this.ValidateAge = function () {

        var min = $jq('#MinAgeTextBox').val();
        var max = $jq('#MaxAgeTextBox').val();
        if (isNaN(min) || min < 18 || min > 120) {
            min = PeopleMedia.Search.defaultMinAge;
        }
        if (isNaN(max) || max > 120 || max < 18) {
            max = PeopleMedia.Search.defaultMaxAge;
        }

        $jq('#MinAgeTextBox').val(min);
        $jq('#MaxAgeTextBox').val(max);
        $jq('[data-property=age]').html('who are ' + min + ' - ' + max);
        //PeopleMedia.Search.defaultMaxAge = max;
        //PeopleMedia.Search.defaultMinAge = min;
    };
    this.ToggleSearchParametersVisible = function (event) {
        if ($jq('#AdvancedSearchParamsDiv').is(":visible")) {
            $jq('#SearchResultsContainer div.searchresults').css('border-top', 'none');
            $jq('.ziperrorplace').fadeOut(PeopleMedia.Search.slideToggleSpeed);
            $jq('#AdvancedSearchParamsDiv').slideToggle(PeopleMedia.Search.slideToggleSpeed);
            $jq('.searchparametershide').slideToggle(PeopleMedia.Search.slideToggleSpeed);
            $jq('.showhidearrowdown').show();
            $jq('.showhidearrowup').hide();

        } else {
            $jq('#SearchResultsContainer div.searchresults').css('border-top', '');
            $jq('.showhidearrowdown').hide();
            $jq('.showhidearrowup').show();
            $jq('.searchparametershide').slideToggle(PeopleMedia.Search.slideToggleSpeed);
            $jq('#AdvancedSearchParamsDiv').slideToggle(PeopleMedia.Search.slideToggleSpeed);
            if (PeopleMedia.Search.InvalidZip) {
                $jq('.ziperrorplace').fadeIn(PeopleMedia.Search.slideToggleSpeed);
            }

        }
    };
    this.CriteriaMoreClicked = function (event) {
        event.stopPropagation();
        var firstChild = $jq(this).find('div[class$=expandedplace] > div');
        $jq('.searchbuttonrepeat > div[class$=expandedplace] > div').each(function () {
            if ($jq(this)[0] != firstChild[0]) {
                $jq(this).hide();
            }
        });

        if (!firstChild.parent().is(":visible")) {
            firstChild.parent().show();
            firstChild.hide();
        }
        firstChild.slideToggle(PeopleMedia.Search.slideToggleSpeed);
    };
    this.AddCriteria = function (event) {
        if (typeof String.prototype.trim !== 'function') {
            String.prototype.trim = function () {
                return this.replace(/^\s+|\s+$/g, '');
            };
        }
        event.stopPropagation();
        var value = $jq(this).find("p:first-child").html().trim().replace(' ', '');
        if (value == 'more') {
            $jq(".searchmoreexpanded").slideToggle(PeopleMedia.Search.slideToggleSpeed);
            return;
        }
        var foundItem = $jq('.search' + value + 'expandedplace');
        $jq(this).hide();
        $jq(".searchmoreexpanded").slideToggle(PeopleMedia.Search.slideToggleSpeed);
        if (foundItem && foundItem.html() != null) {
            foundItem = foundItem.parent();
            var selector = foundItem.clone().insertBefore($jq('.searchmoreexpanded').parent().parent()).show()
                   .click(PeopleMedia.Search.CriteriaMoreClicked).click();
            PeopleMedia.Search.WireUpCriteria(selector.find('.searchparameteroption, .searchparameteroptionlast,.searchparameteroption2, .searchparameteroptionlast2'));
            selector.find('[data-input]').change(PeopleMedia.Search.CriteriaSelectionChanged);
            selector.find('[hide-criteria]').on('click', PeopleMedia.Search.HideCriteria);
            foundItem.remove();
        }
        if (value == 'height') {
            if (PeopleMedia.Search.heightNeedsHardReset) {
                $jq('#MinHeight option:selected').removeAttr('selected');
                $jq('#MaxHeight option:selected').removeAttr('selected');
                $jq('#MinHeight option:first-child').attr('selected', '');
                $jq('#MaxHeight option:last-child').attr('selected', '');
            }
            $jq('#MinHeight, #MaxHeight').change(PeopleMedia.Search.UpdateHeight);
        }
        PeopleMedia.Search.SetLastVisibleCriteriaClass();
    };
    this.SetLastVisibleCriteriaClass = function () {
        $jq('.searchmoreexpanded').show();
        $jq('div[criteria-type]').addClass('searchparameteroption2').removeClass('searchparameteroptionlast2');
        $jq('div[criteria-type]:visible:last').addClass('searchparameteroptionlast2');

        $jq('[criteria-selector]').hide();

        $jq('div[criteria-type]').each(function () {
            if ($jq(this).css('display') != 'none') {
                $jq('[criteria-selector]').show();
            }
        });

        $jq('.searchmoreexpanded').hide();
        var uaBrowser = PeopleMedia.Search.getBrowser();
        if (uaBrowser && uaBrowser.msie && parseInt(uaBrowser.version, 10) == 7) {
            var count = $jq('.search2ndrow > div').length;
            var height = 60;
            if (count <= 4) {
                height = 60;
            } else if (count > 4 && count < 8) {
                height = 90;
            } else {
                height = 120;
            }

            $jq('.searchrow').height(height + 'px');
            $jq('.searchparametersshow').height((75 + height) + 'px');
        }

    };

    this.heightNeedsHardReset = false;
    this.HideCriteria = function (event) {
        event.stopPropagation();
        var value = $jq(this).attr('hide-criteria');
        var $criteria = $jq('[criteria-for=' + value + ']');
        $jq('.searchmoreexpanded').find('[criteria-type=' + value + ']').removeAttr('style');


        var $clone = $criteria.clone();
        $clone.appendTo('#hiddenSearch');
        $criteria.remove();
        if (value == 'height') {
            $jq('#MinHeight option:selected').removeAttr('selected');
            $jq('#MaxHeight option:selected').removeAttr('selected');
            $jq('#MinHeight option:first-child').attr('selected', '');
            $jq('#MaxHeight option:last-child').attr('selected', '');
            PeopleMedia.Search.heightNeedsHardReset = true;
            PeopleMedia.Search.UpdateHeight();
        }
        else {
            $clone.find('input[type=checkbox]').filter('[criteria-text=any]').attr('checked', true);
            $clone.find('input[type=checkbox]').not('[criteria-text=any]').removeAttr('checked');
        }
        PeopleMedia.Search.SetLastVisibleCriteriaClass();
    };
    this.WireUpCriteria = function ($selector) {
        $selector.mouseenter(function () { $jq(this).addClass("searchparameteroptionhover"); })
               .mouseleave(function () { $jq(this).removeClass("searchparameteroptionhover"); })
               .click(function (e) {
                   e.stopPropagation();
                   if (e.target.tagName != 'INPUT' && e.target.tagName != 'LABEL') {
                       var checkbox = $jq(this).find('[data-input]');
                       if (checkbox != undefined) {
                           if (checkbox.attr('type') != 'radio' || !checkbox.attr("checked")) {
                               checkbox.attr("checked", !checkbox.attr("checked"));
                               checkbox.trigger('change');
                           }
                       }
                   }
               });
    };
    this.GetValues = function (base) {
        var items = $jq('[criteria-for=' + base + '] input[type=checkbox]:checked');
        if (items.length > 0) {
            var array = [];
            var current = 0;
            items.each(function () {
                array[current] = $jq(this).val();
                current++;
            });

            return array;
        }
        return [];
    };
    this.SetValues = function (baseType, selectedValues) {
        var items = $jq('[criteria-for=' + baseType + '] input[type=checkbox]');
        var shouldShow = false;
        if (items.length > 0) {
            items.each(function () {
                if (($jq.isArray(selectedValues) && $jq.inArray($jq(this).val(), selectedValues) > -1) || $jq(this).val() === selectedValues) {
                    $jq(this).attr('checked', true);
                    if ($jq(this).val() !== -1) {
                        shouldShow = true;
                    }
                } else {
                    $jq(this).removeAttr('checked');
                }
            });
        }
        if (shouldShow) {
            $jq('[criteria-type=' + baseType + ']:visible').click();
        }
    };
    this.GetParameters = function (isDefault) {
        jQuery.ajaxSettings.traditional = true;

        var sortcol = PeopleMedia.Search.GetQueryVariable("sortCol");
        if (sortcol) {
        }

        var currentselectLocation;
        if (PeopleMedia.Search.searchBy == 1) {
            currentselectLocation = $jq('#ZipTextBox').val();
            PeopleMedia.Search.searchBy = 1;

        }
        else {
            currentselectLocation = $jq('#Location option:selected').val();
            PeopleMedia.Search.searchBy = 0;
        }

        if ($jq('#SearchCountry').is(":visible")) {
            currentselectLocation = $jq('#Location option:selected').val();
            PeopleMedia.Search.searchBy = 0;
        }
        if ($jq('#SearchZip').is(":visible")) {
            currentselectLocation = $jq('#ZipTextBox').val();
            PeopleMedia.Search.searchBy = 1;
        }

        return {
            /*Gender: $jq('#GenderFemale').is(":checked") ? 2 : 1,
            MinAge: $jq('#MinAgeTextBox').val(),
            MaxAge: $jq('#MaxAgeTextBox').val(),
            Location: currentselectLocation,
            MinHeight: $jq('#MinHeight').val(),
            MaxHeight: $jq('#MaxHeight').val(),
            DisplayAsGallery: PeopleMedia.Search.viewGallery,
            ProfilesWith: $jq('#HasPhotosHasPhotos').is(":checked") ? 1 : -1,
            Marital_Status: PeopleMedia.Search.GetValues('status'),
            Ethnicity: PeopleMedia.Search.GetValues('ethnicity'),
            Relationship_Expectations: PeopleMedia.Search.GetValues('lookingfor'),
            Zodiac: PeopleMedia.Search.GetValues('zodiac'),
            Smoking_Preference: PeopleMedia.Search.GetValues('smoking'),
            General_Build: PeopleMedia.Search.GetValues('build'),
            Children: PeopleMedia.Search.GetValues('children'),
            Religion: PeopleMedia.Search.GetValues('religion'),
            Temple_Status: PeopleMedia.Search.GetValues('temple'),
            Mission: PeopleMedia.Search.GetValues('mission'),
            Church_Activity: PeopleMedia.Search.GetValues('churchactivity'),
            Denomination: PeopleMedia.Search.GetValues('denomination'),
            Language: PeopleMedia.Search.GetValues('language'),
            Living_Situation: PeopleMedia.Search.GetValues('living'),
            Complexion: PeopleMedia.Search.GetValues('complexion'),
            Keep_Kosher: PeopleMedia.Search.GetValues('keepkosher'),
            Synagogue_Attendance: PeopleMedia.Search.GetValues('synagogue'),
            Sortby: PeopleMedia.Search.sortCol,
            Start: PeopleMedia.Search.currentPage,
            SearchBy: PeopleMedia.Search.searchBy*/
        };
    };

    this.SetParameters = function (data) {


        if (data.Gender == 2) {
            $jq('#GenderFemale').attr('checked', true);
            $jq('#GenderMale').removeAttr('checked');
        }
        else {
            $jq('#GenderMale').attr('checked', true);
            $jq('#GenderFemale').removeAttr('checked');
        }
        $jq('#MinAgeTextBox').val(data.MinAge);
        $jq('#MaxAgeTextBox').val(data.MaxAge);
        PeopleMedia.Search.ValidateAge();


        if (data.SearchBy === '0') {
            $jq('#Location').val(data.Location);
            $jq('#Location').trigger('change');
            $jq('#SearchCountry').show();
            $jq('#SearchZip').hide();

        }
        else {
            $jq('#ZipTextBox').val(data.Location);
            $jq('#SearchCountry').hide();
            $jq('#SearchZip').show();
            //if (!PeopleMedia.Search.isFirstResult) {
            //    PeopleMedia.Search.ValidateZip();
            //}

        }
        $jq('#MinHeight').val(data.MinHeight);
        $jq('#MaxHeight').val(data.MaxHeight);
        PeopleMedia.Search.UpdateHeight();
        PeopleMedia.Search.viewGallery = data.DisplayAsGallery;
        if (data.ProfilesWith == 1) {
            $jq('#HasPhotosHasPhotos').attr('checked', true);
            $jq('#HasPhotosAny').removeAttr('checked');
        } else {
            $jq('#HasPhotosAny').attr('checked', true);
            $jq('#HasPhotosHasPhotos').removeAttr('checked');
        }
        $jq('input[name=hasphotos]').trigger('change');
        /*PeopleMedia.Search.SetValues('status', data.Marital_Status);
        PeopleMedia.Search.SetValues('ethnicity', data.Ethnicity);
        PeopleMedia.Search.SetValues('lookingfor', data.Relationship_Expectations);
        PeopleMedia.Search.SetValues('zodiac', data.Zodiac);
        PeopleMedia.Search.SetValues('smoking', data.Smoking_Preference);
        PeopleMedia.Search.SetValues('build', data.General_Build);
        PeopleMedia.Search.SetValues('children', data.Children);
        PeopleMedia.Search.SetValues('religion', data.Religion);
        PeopleMedia.Search.SetValues('temple', data.Temple_Status);
        PeopleMedia.Search.SetValues('mission', data.Mission);
        PeopleMedia.Search.SetValues('churchactivity', data.Church_Activity);
        PeopleMedia.Search.SetValues('denomination', data.Denomination);
        PeopleMedia.Search.SetValues('language', data.Language);
        PeopleMedia.Search.SetValues('living', data.Living_Situation);
        PeopleMedia.Search.SetValues('complexion', data.Complexion);
        PeopleMedia.Search.SetValues('keepkosher', data.Keep_Kosher);
        PeopleMedia.Search.SetValues('synagogue', data.Synagogue_Attendance);
        PeopleMedia.Search.sortCol = data.Sortby;
        PeopleMedia.Search.currentPage = data.Start;
        PeopleMedia.Search.searchBy = data.SearchBy;*/
    };
    this.GetQueryVariable = function (variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
    };
    this.noHash = false;
    this.SetSortCol = function (col) {
        PeopleMedia.Search.noHash = true;
        PeopleMedia.Search.sortCol = col;
        if (PeopleMedia.Search.isUsernameSearch) {
            $jq.bbq.pushState(PeopleMedia.Search.GetUsernameData());
        } else {
            $jq.bbq.pushState(PeopleMedia.Search.GetParameters(false));
        }
    };
    this.SetViewGallery = function (viewGal) {
        PeopleMedia.Search.noHash = true;
        PeopleMedia.Search.viewGallery = viewGal;
        if (PeopleMedia.Search.isUsernameSearch) {
            $jq.bbq.pushState(PeopleMedia.Search.GetUsernameData());
        } else {
            $jq.bbq.pushState(PeopleMedia.Search.GetParameters(false));
        }
    };
    this.SetCurrentPage = function (page) {
        PeopleMedia.Search.noHash = true;
        PeopleMedia.Search.currentPage = page;
        if (PeopleMedia.Search.isUsernameSearch) {
            $jq.bbq.pushState(PeopleMedia.Search.GetUsernameData());
        } else {
            $jq.bbq.pushState(PeopleMedia.Search.GetParameters(false));
        }
    };
    this.ValidateCriteria = function () {
        var minVal = $jq('#MinHeight').val();
        var maxVal = $jq('#MaxHeight').val();
        if (minVal > maxVal) {
            $jq('#MinHeight').val(maxVal);
            $jq('#MaxHeight').val(minVal);
        }
        PeopleMedia.Search.UpdateHeight();
        var min = $jq('#MinAgeTextBox').val();
        var max = $jq('#MaxAgeTextBox').val();
        if (isNaN(min) || min < 18 || min > 120) {
            min = PeopleMedia.Search.defaultMinAge;
        }
        if (isNaN(max) || max > 120 || max < 18) {
            max = PeopleMedia.Search.defaultMaxAge;
        }
        if (parseInt(max, 10) < parseInt(min, 10)) {
            var temp = max;
            max = min;
            min = temp;
            $jq('#MinAgeTextBox').val(min);
            $jq('#MaxAgeTextBox').val(max);
        }
        PeopleMedia.Search.ValidateAge();
    };
    this.resetPage = false;
    this.SearchButtonClicked = function () {
        PeopleMedia.Search.resetPage = true;
        PeopleMedia.Search.ExecuteSearch();
    };
    this.ExecuteSearch = function () {
        if (PeopleMedia.Search.isUsernameSearch) {
            PeopleMedia.Search.ExecuteUsernameSearch();
            return;
        }
        PeopleMedia.Search.ValidateCriteria();
        var data = PeopleMedia.Search.GetParameters(false);
        $jq.bbq.pushState(data, 2);
    };
    this.ExecuteDefaultSearch = function () {
        var data = $jq.bbq.getState();
        if (data.Location == undefined) {
            var start = data.Start || 1;
            var sort = data.Sortby || 'MyDefault';
            data = PeopleMedia.Search.GetParameters(true);
            data.Start = start;
            data.Sortby = sort;
            $jq.bbq.pushState(data, 2);
            var uaBrowser = PeopleMedia.Search.getBrowser();
            if (uaBrowser && uaBrowser.msie && parseInt(uaBrowser.version, 10) == 7) {
                $jq(window).trigger('hashchange');
            }
        } else {
            PeopleMedia.Search.SetParameters(data);
            var newData = PeopleMedia.Search.GetParameters(false);
            $jq.bbq.pushState(newData, 2);
            PeopleMedia.Search.IsDefaultSearch = false;
            $jq(window).trigger('hashchange');
        }
    };
    this.ResetCriteria = function () {
        if (PeopleMedia.Search.isUsernameSearch) {
            $jq('#UsernameSearchTextBox').focus();
            return;
        }

        $jq('.search2ndrow [criteria-for] div:last-child [hide-criteria]').click();
        PeopleMedia.Search.SearchButtonClicked();
    };
    this.ShowSearchCriteria = function () {
        $jq('.showhidearrowup').show();
        $jq('.showhidearrowdown').hide();
        $jq('.searchparametersshow').show();
    };
    this.WireUpSearchResults = function (html) {
        var state = $jq.bbq.getState();
        $jq('#SearchResultsContainer').html(html);
        $jq('#SearchResultsContainer').show();
        $jq('#ResetSearchCriteriaButton').click(PeopleMedia.Search.ResetCriteria);
        if (PeopleMedia.Search.isUsernameSearch) {
            $jq('#ExpandMySearchCriteria').click(PeopleMedia.Search.ResetCriteria);
        }
        else {
            $jq('#ExpandMySearchCriteria').click(PeopleMedia.Search.ShowSearchCriteria);
        }


        var result = $jq('#SearchResultsContainer');
        if (!$jq('#AdvancedSearchParamsDiv').is(":visible")) {
            $jq('#SearchResultsContainer div.searchresults').css('border-top', 'none');
        }
        result.find('#DisplayListLink').click(function () {
            PeopleMedia.Search.viewGallery = false;
            PeopleMedia.Search.currentPage = 1;
            if (PeopleMedia.Search.isUsernameSearch) {
                $jq.bbq.pushState(PeopleMedia.Search.GetUsernameData());
            } else {
                $jq.bbq.pushState(PeopleMedia.Search.GetParameters(false));
            }
        });
        result.find('#DisplayGalleryLink').click(function () {
            PeopleMedia.Search.viewGallery = true;
            PeopleMedia.Search.currentPage = 1;
            if (PeopleMedia.Search.isUsernameSearch) {
                $jq.bbq.pushState(PeopleMedia.Search.GetUsernameData());
            } else {
                $jq.bbq.pushState(PeopleMedia.Search.GetParameters(false));
            }
        });
        result.find('[bday-member-id]').click(PeopleMedia.Search.SendBirthdayFlirt);
        result.find('[page-index]').click(function () {
            PeopleMedia.Search.currentPage = $jq(this).attr('page-index');
            PeopleMedia.Search.resetPage = false;
            PeopleMedia.Search.ExecuteSearch();
        });

        result.find('[fav-member-id]').click(PeopleMedia.Search.AddFavorite);
        result.find('[unfav-member-id]').click(PeopleMedia.Search.RemoveFavorite);
        result.find('#searchsortby').change(function () {
            PeopleMedia.Search.sortCol = $jq(this).find('option:selected').val();
            PeopleMedia.Search.currentPage = 1;
            PeopleMedia.Search.ExecuteSearch();
        });
        if (PeopleMedia.Search.isFirstResult == true) {
            PeopleMedia.Search.isFirstResult = false;
        } else {
            PeopleMedia.ReloadAds();
        }
    };
    this.AddFavorite = function () {
        $jq(this).unbind('click');
        var encodedMemberId = $jq(this).attr('fav-member-id');
        var other = $jq('[unfav-member-id=' + encodedMemberId + ']');
        $jq(this).hide();
        other.show();
        other.click(PeopleMedia.Search.RemoveFavorite);
        $jq.ajax({
            type: 'POST',
            url: "/v3/Ajax/SaveToFavorites", //Unfavorite
            data: { EncodedMemberId: encodedMemberId,
                FeatureName: PeopleMedia.Search.isUsernameSearch ? PeopleMedia.Search.UsernameFeatureName : PeopleMedia.Search.FeatureName
            },
            success: function (response) {
                if (response.status == "Success") {

                } else {
                    $jq(this).show();
                    $jq(this).click(PeopleMedia.Search.AddFavorite);
                    other.hide();
                    other.unbind();
                    PeopleMedia.Profile.handleError(response);
                }
            }
        });
    };
    this.RemoveFavorite = function () {
        $jq(this).unbind('click');
        var encodedMemberId = $jq(this).attr('unfav-member-id');
        var other = $jq('[fav-member-id=' + encodedMemberId + ']');
        $jq(this).hide();
        other.show();
        other.click(PeopleMedia.Search.AddFavorite);
        $jq.ajax({
            type: 'POST',
            url: "/v3/Ajax/UnFavorite",
            data: {
                EncodedMemberId: encodedMemberId,
                FeatureName: PeopleMedia.Search.isUsernameSearch ? PeopleMedia.Search.UsernameFeatureName : PeopleMedia.Search.FeatureName
            },
            success: function (response) {
                if (response.status == "Success") {

                } else {
                    $jq(this).show();
                    $jq(this).click(PeopleMedia.Search.RemoveFavorite);
                    other.hide();
                    other.unbind();
                    PeopleMedia.Profile.handleError(response);
                }
            }
        });
    };
    this.SendBirthdayFlirt = function () {
        var encodedMemberId = $jq(this).attr('bday-member-id');
        $jq(this).parent().html("<a>Birthday wish sent.</a>");
        $jq.ajax({
            type: 'POST',
            url: "/v3/Ajax/SendBirthdayFlirt",
            data: {
                EncodedMemberId: encodedMemberId,
                FeatureName: PeopleMedia.Search.isUsernameSearch ? PeopleMedia.Search.UsernameFeatureName : PeopleMedia.Search.FeatureName
            },
            success: function (response) {
                if (response.status == "Success") {

                } else {
                    PeopleMedia.Profile.handleError(response);
                }
            }
        });
    };
    this.SetDefaultUserNameSearched = function (username) {
        $jq('#UsernameSearchTextBox').val(username.replace(/[^a-z0-9]/gi, ''));

        $jq.bbq.pushState(PeopleMedia.Search.GetUsernameData(), 2);
    };
    this.isUsernameSearch = false;
    this.isUsernameSearchExactMatch = true;
    this.isUsernameSearchWired = false;
    this.usernameHash = null;
    this.ShowUsernameSearch = function (noPush) {
        if (PeopleMedia.Search.isUsernameSearch == true) {
            return;
        }
        if (PeopleMedia.Search.isSearching == true) {
            return;
        }
        if (PeopleMedia.Search.usernameHash != null) {
            PeopleMedia.Search.currentPage = PeopleMedia.Search.usernameHash.Start;
        } else {
            PeopleMedia.Search.currentPage = 1;
        }
        if (!noPush) {
            PeopleMedia.Search.keywordHash = $jq.bbq.getState();
            if (PeopleMedia.Search.usernameHash !== null) {
                $jq.bbq.pushState(PeopleMedia.Search.usernameHash, 2);
            } else {
                $jq.bbq.pushState(PeopleMedia.Search.GetUsernameData(), 2);
            }
        }


        $jq('.ziperrorplace').fadeOut(PeopleMedia.Search.slideToggleSpeed);
        if ($jq('#SearchResultsContainer').is(":visible")) {
            $jq('#SearchResultsContainer').hide();
        } else {

        }
        $jq('#searchmoreexpanded').hide();
        $jq('#SearchTab').removeClass('menufindareaon').addClass('menufindareaoff color-main');
        $jq('#OnlineNowSearchTab').removeClass('menufindareaon').addClass('menufindareaoff color-main');
        $jq('#UsernameSearchTab').addClass('menufindareaon').removeClass('menufindareaoff color-main');
        if ($jq('#AdvancedSearchParamsDiv').is(":visible")) {
            $jq('#AdvancedSearchParamsDiv').slideToggle(PeopleMedia.Search.slideToggleSpeed);
        }
        $jq('.showhidearrowup').hide();
        $jq('.showhidearrowdown').hide();
        $jq('#UsernameSearchParamsDiv').slideToggle(PeopleMedia.Search.slideToggleSpeed, function () { $jq('#UsernameSearchTextBox').focus(); });
        PeopleMedia.Search.isUsernameSearch = true;
        $jq('#SearchTab').click(PeopleMedia.Search.HideUsernameSearch);
        if (PeopleMedia.Search.isUsernameSearchWired === false) {
            PeopleMedia.Search.isUsernameSearchWired = true;
            $jq('#UsernameSearchTextBox').keyup(function (event) {
                $jq('#UsernameSearchButton').addClass('buttonunavailable').unbind();
                var username = $jq('#UsernameSearchTextBox').val();
                if (username.length < 3) {
                    return;
                }
                $jq('#UsernameSearchButton').removeClass('buttonunavailable').click(PeopleMedia.Search.ExecuteUsernameSearch);
                if (event.keyCode == 13) {
                    PeopleMedia.Search.ExecuteUsernameSearch();
                }
            });

            $jq('#UsernameSearchExactMatchLink').click(function () {
                if (PeopleMedia.Search.isUsernameSearchExactMatch) { return; }
                PeopleMedia.Search.isUsernameSearchExactMatch = true;
                $jq('#UsernameSearchExactMatchLink').parent().parent().removeClass('listpagelinksarea1off').addClass('listpagelinksarea1on');
                $jq('#UsernameSearchSimilarMatchLink').parent().parent().removeClass('listpagelinksarea3on').addClass('listpagelinksarea3off');

                var username = $jq('#UsernameSearchTextBox').val();
                if (username.length < 3) {
                    $jq('#UsernameSearchButton').addClass('buttonunavailable').unbind('click');
                } else {
                    $jq('#UsernameSearchButton').removeClass('buttonunavailable').click(PeopleMedia.Search.ExecuteUsernameSearch);
                }
            });
            $jq('#UsernameSearchSimilarMatchLink').click(function () {
                if (!PeopleMedia.Search.isUsernameSearchExactMatch) { return; }
                PeopleMedia.Search.isUsernameSearchExactMatch = false;
                $jq('#UsernameSearchExactMatchLink').parent().parent().removeClass('listpagelinksarea1on').addClass('listpagelinksarea1off');
                $jq('#UsernameSearchSimilarMatchLink').parent().parent().removeClass('listpagelinksarea3off').addClass('listpagelinksarea3on');

                var username = $jq('#UsernameSearchTextBox').val();
                if (username.length < 3) {
                    $jq('#UsernameSearchButton').addClass('buttonunavailable').unbind('click');
                } else {
                    $jq('#UsernameSearchButton').removeClass('buttonunavailable').click(PeopleMedia.Search.ExecuteUsernameSearch);
                }
            });

        }

    };
    this.processSearchResult = function (result) {
        $jq('#LoadingResultsDiv').fadeOut();
        PeopleMedia.Search.isSearching = false;
        if (result.redirect) {
            location.href = result.redirect;
        } else if (result.status == "Success" || result.status == "Error") {
            PeopleMedia.Search.WireUpSearchResults(result.html);
            PeopleMedia.Timer.StartCountdownTimer();
        } else {
            if (result.message == "Invalid Zip Code") {
                $jq('.ziperrorplace').fadeIn(PeopleMedia.Search.slideToggleSpeed);
            } else {
                PeopleMedia.Popups.ShowGenericMessage(result.message);
            }
        }
    };
    this.ExecuteUsernameSearch = function () {
        var username = $jq('#UsernameSearchTextBox').val();
        if (username == undefined || username.length < 3) {
            PeopleMedia.Popups.ShowGenericMessage('Please enter at least 3 letters to search for a username.');
            return;
        }
        $jq.bbq.pushState(PeopleMedia.Search.GetUsernameData(), 2);
    };
    this.doUsernameSearch = function (ignoreInput) {
        if (PeopleMedia.Search.isSearching == true) return;
        PeopleMedia.Search.isSearching = true;
        $jq('#ExecuteUsernameSearchButton').addClass('buttonunavailable');
        $jq('#ExecuteUsernameSearchButton').unbind('click');
        $jq('#LoadingResultsDiv').fadeIn();
        $jq('#SearchResultsContainer').hide();
        var username = $jq.bbq.getState('Username');

        if (!ignoreInput) {
            if (username == undefined || username.length < 3) {
                PeopleMedia.Popups.ShowGenericMessage('Please enter at least 3 letters to search for a username.');
                return;
            }
        }
        var data = $jq.bbq.getState();
        var startTime = new Date().getTime();
        var url = '/v3/search/executeusernameasync';
        var timeoutValue = 60000;
        $jq.ajax({
            type: "POST",
            url: url,
            data: data,
            dataType: "json",
            success: function (result) {
                $jq('#ExecuteUsernameSearchButton').removeClass('buttonunavailable');
                $jq('#ExecuteUsernameSearchButton').click(PeopleMedia.Search.ExecuteUsernameSearch);
                PeopleMedia.Search.processSearchResult(result);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                if (errorThrown === 'timeout' || (jqXHR.readyState !== 0 && jqXHR.status !== 0)) {
                    var endTime = new Date().getTime();
                    var duration = (endTime - startTime);
                    // PeopleMedia.Popups.ShowGenericMessage(PeopleMedia.Popups.AjaxErrorMessage());
                    PeopleMedia.Logger.LogAjaxErrorXHR("Ajax request failure", url, { method: "PeopleMedia.Search.doUsernameSearch", duration: duration }, jqXHR, textStatus, errorThrown, function () {
                    });
                }
                PeopleMedia.Search.isSearching = false;
                $jq('#LoadingResultsDiv').fadeOut();
                $jq('#ExecuteUsernameSearchButton').removeClass('buttonunavailable');
                $jq('#ExecuteUsernameSearchButton').click(PeopleMedia.Search.ExecuteUsernameSearch);
            },
            async: true,
            cache: false,
            timeout: timeoutValue,
            suppressErrors: true
        });
    };
    this.GetUsernameData = function () {
        return {
            Username: $jq('#UsernameSearchTextBox').val(),
            Gender: $jq('#UsernameGenderFemale').is(":checked") ? 2 : 1,
            MatchStyle: PeopleMedia.Search.isUsernameSearchExactMatch ? 'Exact' : 'Similar',
            Start: PeopleMedia.Search.currentPage,
            DisplayAsGallery: PeopleMedia.Search.viewGallery
        };
    };
    this.SetUsernameData = function (data) {
        $jq('#UsernameSearchTextBox').val(data.Username);
        if (data.Gender == 2) {
            $jq('#UsernameGenderFemale').attr('checked', true);
            $jq('#UsernameGenderMale').removeAttr('checked');
        } else if (data.Gender == 1) {
            $jq('#UsernameGenderMale').attr('checked', true);
            $jq('#UsernameGenderFemale').removeAttr('checked');
        }

        // added to force default username selection to "Exact"
        if (typeof (data.MatchStyle) === "undefined" || data.MatchStyle === null || (data.MatchStyle !== 'Exact' && data.MatchStyle !== 'Similar')) {
            data.MatchStyle = 'Exact';
        }

        if (typeof (data.DisplayAsGallery) === "undefined" || data.DisplayAsGallery === null) {
            data.DisplayAsGallery = false;
        }

        PeopleMedia.Search.isUsernameSearchExactMatch = data.MatchStyle === 'Exact';
        PeopleMedia.Search.currentPage = data.Start;
        PeopleMedia.Search.viewGallery = data.DisplayAsGallery;
        if (data.MatchStyle === 'Exact') {
            $jq('#UsernameSearchExactMatchLink').parent().parent().removeClass('listpagelinksarea1off').addClass('listpagelinksarea1on');
            $jq('#UsernameSearchSimilarMatchLink').parent().parent().removeClass('listpagelinksarea3on').addClass('listpagelinksarea3off');
        } else {
            $jq('#UsernameSearchExactMatchLink').parent().parent().removeClass('listpagelinksarea1on').addClass('listpagelinksarea1off');
            $jq('#UsernameSearchSimilarMatchLink').parent().parent().removeClass('listpagelinksarea3off').addClass('listpagelinksarea3on');
        }
    };
    this.HideUsernameSearch = function (noPush) {
        if (PeopleMedia.Search.isUsernameSearch == false)
        { return; }
        if (PeopleMedia.Search.isSearching == true) {
            return;
        }
        PeopleMedia.Search.isUsernameSearch = false;
        PeopleMedia.Search.usernameHash = $jq.bbq.getState();
        if (PeopleMedia.Search.sortCol == 'MembersOnline') {
            PeopleMedia.Search.sortCol = 'MyDefault';
        }
        if (PeopleMedia.Search.keywordHash != null)
        { PeopleMedia.Search.currentPage = PeopleMedia.Search.keywordHash.Start; }
        else {
            PeopleMedia.Search.currentPage = 1;
        }
        if (noPush !== true) {
            if (PeopleMedia.Search.keywordHash !== null) {
                $jq.bbq.pushState(PeopleMedia.Search.keywordHash, 2);
            } else {
                $jq.bbq.pushState(PeopleMedia.Search.GetParameters(), 2);
            }
        }


        $jq('#SearchTab').unbind();
        $jq('.showhidearrowup').show();
        $jq('#SearchTab').removeClass('menufindareaoff color-main').addClass('menufindareaon');
        $jq('#UsernameSearchTab').addClass('menufindareaoff color-main').removeClass('menufindareaon');
        $jq('#UsernameSearchParamsDiv').slideToggle(PeopleMedia.Search.slideToggleSpeed);
        $jq('#AdvancedSearchParamsDiv').slideToggle(PeopleMedia.Search.slideToggleSpeed);
        if (PeopleMedia.Search.InvalidZip == false) {
            PeopleMedia.Search.ExecuteSearch();
        } else {
            $jq('.ziperrorplace').fadeIn(PeopleMedia.Search.slideToggleSpeed);
        }
    };

    this.doKeywordSearch = function () {
        if (PeopleMedia.Search.isSearching == true) return;
        PeopleMedia.Search.isSearching = true;
        $jq('#ExecuteSearchButton').addClass('buttonunavailable');
        $jq('#ExecuteSearchButton').unbind('click');
        $jq('#LoadingResultsDiv').fadeIn();
        $jq('#SearchResultsContainer').hide();
        var state = $jq.bbq.getState();
        state.IsDefaultSearch = PeopleMedia.Search.IsDefaultSearch;

        var startTime = new Date().getTime();
        var url = '/v3/search/executeasync';
        var timeoutValue = 60000;
        $jq.ajax({
            type: "POST",
            url: url,
            data: state,
            dataType: "json",
            success: function (result) {
                $jq('#ExecuteSearchButton').removeClass('buttonunavailable');
                $jq('#ExecuteSearchButton').click(PeopleMedia.Search.SearchButtonClicked);
                PeopleMedia.Search.processSearchResult(result);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                if (errorThrown === 'timeout' || (jqXHR.readyState !== 0 && jqXHR.status !== 0)) {
                    var endTime = new Date().getTime();
                    var duration = (endTime - startTime);
                    // PeopleMedia.Popups.ShowGenericMessage(PeopleMedia.Popups.AjaxErrorMessage());
                    PeopleMedia.Logger.LogAjaxErrorXHR("Ajax request failure", url, { method: "PeopleMedia.Search.doKeywordSearch", duration: duration }, jqXHR, textStatus, errorThrown, function () {
                    });
                }
                PeopleMedia.Search.isSearching = false;
                $jq('#LoadingResultsDiv').fadeOut();
                $jq('#ExecuteSearchButton').removeClass('buttonunavailable');
                $jq('#ExecuteSearchButton').click(PeopleMedia.Search.SearchButtonClicked);
            },
            async: true,
            cache: false,
            timeout: timeoutValue,
            suppressErrors: true
        });
        PeopleMedia.Search.IsDefaultSearch = false;
    };
    this.IsDefaultSearch = true;
    this.keywordHash = null;

    this.hashchange = function (event) {
        var data = $jq.bbq.getState();
        if (PeopleMedia.Search.resetPage) {
            PeopleMedia.Search.resetPage = false;
            if (data.Start != 1) {
                data.Start = 1;
                $jq.bbq.pushState(data);
                return;
            }
        }
        if (data.Username != undefined) {
            PeopleMedia.Search.SetUsernameData(data);
            PeopleMedia.Search.usernameHash = data;
            if (!PeopleMedia.Search.isUsernameSearch) {
                if (PeopleMedia.Search.usernameHash && PeopleMedia.Search.usernameHash.Start) {
                    PeopleMedia.Search.currentPage = PeopleMedia.Search.usernameHash.Start;
                } else {
                    PeopleMedia.Search.currentPage = 1;
                }
                PeopleMedia.Search.ShowUsernameSearch(true);
            }
            if (data.Username !== '') {
                PeopleMedia.Search.doUsernameSearch();
            }
        }
        else {
            if (data != undefined && data.Location == undefined) {
                PeopleMedia.Search.ExecuteDefaultSearch();
            }
            else if ($jq.param.fragment() === '') {
                $jq('#ExecuteSearchButton').removeClass('buttonunavailable').click(PeopleMedia.Search.SearchButtonClicked);
            }
            else {
                if (PeopleMedia.Search.isUsernameSearch) {
                    PeopleMedia.Search.HideUsernameSearch(true);
                    if (PeopleMedia.Search.keywordHash && PeopleMedia.Search.keywordHash.Start) {
                        PeopleMedia.Search.currentPage = PeopleMedia.Search.keywordHash.Start;
                    } else {
                        PeopleMedia.Search.currentPage = 1;
                    }
                }
                PeopleMedia.Search.keywordHash = data;
                PeopleMedia.Search.SetParameters(data);
                PeopleMedia.Search.doKeywordSearch();
            }
        }
        PeopleMedia.Search.SetOnlineSearch();
    };
    this.SetOnlineSearch = function () {
        var menu;
        var data = $jq.bbq.getState();
        if (data.Sortby == 'MembersOnline' && data.Username == undefined) {
            menu = $jq('div.menufindarea');
            menu.find('div.menusearch').removeClass('menufindareaon').addClass('menufindareaoff color-main');
            $jq('#OnlineNowSearchTab').addClass('menufindareaon').removeClass('menufindareaoff color-main');
            $jq('#SearchTab').unbind().click(function () {
                if (PeopleMedia.Search.isSearching == true) {
                    return;
                }
                $jq.bbq.pushState({ Sortby: 'MyDefault', Start: 1, IsDefaultSearch: false });
            });
        } else if (data.Username == undefined) {
            menu = $jq('div.menufindarea');
            menu.find('div.menusearch').removeClass('menufindareaon').addClass('menufindareaoff color-main');
            $jq('#SearchTab').addClass('menufindareaon').removeClass('menufindareaoff color-main');
        }
    };
    this.uaMatch = function (ua) {
        ua = ua.toLowerCase();

        var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
            /(webkit)[ \/]([\w.]+)/.exec(ua) ||
            /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
            /(msie) ([\w.]+)/.exec(ua) ||
            ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
            [];

        return {
            browser: match[1] || "",
            version: match[2] || "0"
        };
    };
    this.getBrowser = function () {
        var matched, browser;
        matched = PeopleMedia.Search.uaMatch(navigator.userAgent);
        browser = {};
        if (matched.browser) {
            browser[matched.browser] = true;
            browser.version = matched.version;
        }

        // Chrome is Webkit, but Webkit is also Safari.
        if (browser.chrome) {
            browser.webkit = true;
        } else if (browser.webkit) {
            browser.safari = true;
        }

        return browser;
    };
};